﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArtemisTest
{
    public static class Misc
    {
        static int _nComponentsNotFoundPerTick = 0;
        public static void CountComponentNotFound() { _nComponentsNotFoundPerTick++; }
        public static int GetComponentsNotFoundThisTick() { return _nComponentsNotFoundPerTick; }
        public static void ResetComponentsNotFoundThisTick() { _nComponentsNotFoundPerTick = 0; }
    }

    public struct xy
    {
        public float _x;
        public float _y;
        public xy(float X, float Y) { _x = X; _y = Y; }
    }

    public struct AABB
    {
        public xy _center;
        public xy _halfdimension;

        public AABB(xy centre, xy halfdim)
        {
            _center = centre;
            _halfdimension = halfdim;
        }

        public AABB(float x, float y, float dx, float dy)
        {
            _center = new xy(x + dx / 2, y + dy / 2);
            _halfdimension = new xy(dx / 2, dy / 2);
        }

        public void GrowByFactor(float f)
        {
            _halfdimension._x *= f;
            _halfdimension._y *= f;
        }

        public bool IntersectsWithTolerance(AABB other, float pixelTol)
        {
            return
                !(_center._x - _halfdimension._x + pixelTol > other._center._x + other._halfdimension._x - pixelTol ||
                    _center._x + _halfdimension._x - pixelTol < other._center._x - other._halfdimension._x + pixelTol ||
                    _center._y - _halfdimension._y + pixelTol > other._center._y + other._halfdimension._y - pixelTol ||
                    _center._y + _halfdimension._y - pixelTol < other._center._y - other._halfdimension._y + pixelTol);
        }

        public bool Intersects(AABB other)
        {
            return
                !(_center._x - _halfdimension._x > other._center._x + other._halfdimension._x ||
                    _center._x + _halfdimension._x < other._center._x - other._halfdimension._x ||
                    _center._y - _halfdimension._y > other._center._y + other._halfdimension._y ||
                    _center._y + _halfdimension._y < other._center._y - other._halfdimension._y);
        }

        public bool ContainsPoint(xy p)
        {
            return
                p._x >= _center._x - _halfdimension._x &&
                p._x <= _center._x + _halfdimension._x &&
                p._y >= _center._y - _halfdimension._y &&
                p._y <= _center._y + _halfdimension._y;
        }
    }

}
